home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / tcl / tcl70b2.lha / tcl7.0b2 / doc / TraceVar.3 < prev    next >
Text File  |  1993-05-03  |  15KB  |  362 lines

  1. '\"
  2. '\" Copyright (c) 1989-1993 The Regents of the University of California.
  3. '\" All rights reserved.
  4. '\"
  5. '\" Permission is hereby granted, without written agreement and without
  6. '\" license or royalty fees, to use, copy, modify, and distribute this
  7. '\" documentation for any purpose, provided that the above copyright
  8. '\" notice and the following two paragraphs appear in all copies.
  9. '\"
  10. '\" IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
  11. '\" FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  12. '\" ARISING OUT OF THE USE OF THIS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  13. '\" CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. '\"
  15. '\" THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  16. '\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  17. '\" AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  18. '\" ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  19. '\" PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  20. '\" 
  21. '\" $Header: /user6/ouster/tcl/man/RCS/TraceVar.3,v 1.14 93/05/03 15:53:18 ouster Exp $ SPRITE (Berkeley)
  22. '\" 
  23. .so man.macros
  24. .HS Tcl_TraceVar tclc
  25. .BS
  26. .SH NAME
  27. Tcl_TraceVar, Tcl_TraceVar2, Tcl_UntraceVar, Tcl_UntraceVar2, Tcl_VarTraceInfo, Tcl_VarTraceInfo2 \- monitor accesses to a variable
  28. .SH SYNOPSIS
  29. .nf
  30. \fB#include <tcl.h>\fR
  31. .sp
  32. int
  33. \fBTcl_TraceVar(\fIinterp, varName, flags, proc, clientData\fB)\fR
  34. .sp
  35. int
  36. \fBTcl_TraceVar2(\fIinterp, name1, name2, flags, proc, clientData\fB)\fR
  37. .sp
  38. \fBTcl_UntraceVar(\fIinterp, varName, flags, proc, clientData\fB)\fR
  39. .sp
  40. \fBTcl_UntraceVar2(\fIinterp, name1, name2, flags, proc, clientData\fB)\fR
  41. .sp
  42. ClientData
  43. \fBTcl_VarTraceInfo(\fIinterp, varName, flags, proc, prevClientData\fB)\fR
  44. .sp
  45. ClientData
  46. \fBTcl_VarTraceInfo2(\fIinterp, name1, name2, flags, proc, prevClientData\fB)\fR
  47. .SH ARGUMENTS
  48. .AS Tcl_VarTraceProc prevClientData
  49. .AP Tcl_Interp *interp in
  50. Interpreter containing variable.
  51. .AP char *varName in
  52. Name of variable.  May refer to a scalar variable, to
  53. an array variable with no index, or to an array variable
  54. with a parenthesized index.
  55. .AP int flags in
  56. OR-ed combination of the values TCL_TRACE_READS, TCL_TRACE_WRITES, and
  57. TCL_TRACE_UNSETS, and TCL_GLOBAL_ONLY.  Not all flags are used by all
  58. procedures.  See below for more information.
  59. .AP Tcl_VarTraceProc *proc in
  60. Procedure to invoke whenever one of the traced operations occurs.
  61. .AP ClientData clientData in
  62. Arbitrary one-word value to pass to \fIproc\fR.
  63. .AP char *name1 in
  64. Name of scalar or array variable (without array index).
  65. .AP char *name2 in
  66. For a trace on an element of an array, gives the index of the
  67. element.  For traces on scalar variables or on whole arrays,
  68. is NULL.
  69. .AP ClientData prevClientData in
  70. If non-NULL, gives last value returned by \fBTcl_VarTraceInfo\fR or
  71. \fBTcl_VarTraceInfo2\fR, so this call will return information about
  72. next trace.  If NULL, this call will return information about first
  73. trace.
  74. .BE
  75.  
  76. .SH DESCRIPTION
  77. .PP
  78. \fBTcl_TraceVar\fR allows a C procedure to monitor and control
  79. access to a Tcl variable, so that the C procedure is invoked
  80. whenever the variable is read or written or unset.
  81. If the trace is created successfully then \fBTcl_TraceVar\fR returns
  82. TCL_OK.  If an error occurred (e.g. \fIvarName\fR specifies an element
  83. of an array, but the actual variable isn't an array) then TCL_ERROR
  84. is returned and an error message is left in \fIinterp->result\fR.
  85. .PP
  86. The \fIflags\fR argument to \fBTcl_TraceVar\fR indicates when the
  87. trace procedure is to be invoked and provides information
  88. for setting up the trace.  It consists of an OR-ed combination
  89. of any of the following values:
  90. .TP
  91. \fBTCL_GLOBAL_ONLY\fR
  92. Normally, the variable will be looked up at the current level of
  93. procedure call;  if this bit is set then the variable will be looked
  94. up at global level, ignoring any active procedures.
  95. .TP
  96. \fBTCL_TRACE_READS\fR
  97. Invoke \fIproc\fR whenever an attempt is made to read the variable.
  98. .TP
  99. \fBTCL_TRACE_WRITES\fR
  100. Invoke \fIproc\fR whenever an attempt is made to modify the variable.
  101. .TP
  102. \fBTCL_TRACE_UNSETS\fR
  103. Invoke \fIproc\fR whenever the variable is unset.
  104. A variable may be unset either explicitly by an \fBunset\fR command,
  105. or implicitly when a procedure returns (its local variables are
  106. automatically unset) or when the interpreter is deleted (all
  107. variables are automatically unset).
  108. .PP
  109. Whenever one of the specified operations occurs on the variable,
  110. \fIproc\fR will be invoked.
  111. It should have arguments and result that match the type
  112. \fBTcl_VarTraceProc\fR:
  113. .nf
  114. .RS
  115. typedef char *Tcl_VarTraceProc(
  116. .RS
  117. ClientData \fIclientData\fR,
  118. Tcl_Interp *\fIinterp\fR,
  119. char *\fIname1\fR,
  120. char *\fIname2\fR,
  121. int \fIflags\fR);
  122. .RE
  123. .RE
  124. .fi
  125. The \fIclientData\fP and \fIinterp\fP parameters will
  126. have the same values as those passed to \fBTcl_TraceVar\fR when the
  127. trace was created.
  128. \fIClientData\fR typically points to an application-specific
  129. data structure that describes what to do when \fIproc\fR
  130. is invoked.
  131. \fIName1\fR and \fIname2\fR give the name of the traced variable
  132. in the normal two-part form (see the description of \fBTcl_TraceVar2\fR
  133. below for details).
  134. \fIFlags\fR is an OR-ed combination of bits providing several
  135. pieces of information.
  136. One of the bits TCL_TRACE_READS, TCL_TRACE_WRITES, or TCL_TRACE_UNSETS
  137. will be set in \fIflags\fR to indicate which operation is being performed
  138. on the variable.
  139. The bit TCL_GLOBAL_ONLY will be set whenever the variable being
  140. accessed is a global one not accessible from the current level of
  141. procedure call:  the trace procedure will need to pass this flag
  142. back to variable-related procedures like \fBTcl_GetVar\fR if it
  143. attempts to access the variable.
  144. The bit TCL_TRACE_DESTROYED will be set in \fIflags\fR if the trace is
  145. about to be destroyed;  this information may be useful to \fIproc\fR
  146. so that it can clean up its own internal data structures (see
  147. the section TCL_TRACE_DESTROYED below for more details).
  148. Lastly, the bit TCL_INTERP_DESTROYED will be set if the entire
  149. interpreter is being destroyed.
  150. When this bit is set, \fIproc\fR must be especially careful in
  151. the things it does (see the section TCL_INTERP_DESTROYED below).
  152. The trace procedure's return value should normally be NULL;  see
  153. ERROR RETURNS below for information on other possibilities.
  154. .PP
  155. \fBTcl_UntraceVar\fR may be used to remove a trace.
  156. If the variable specified by \fIinterp\fR, \fIvarName\fR, and \fIflags\fR
  157. has a trace set with \fIflags\fR, \fIproc\fR, and
  158. \fIclientData\fR, then the corresponding trace is removed.
  159. If no such trace exists, then the call to \fBTcl_UntraceVar\fR
  160. has no effect.
  161. The same bits are valid for \fIflags\fR as for calls to \fBTcl_TraceVars\fR.
  162. .PP
  163. \fBTcl_VarTraceInfo\fR may be used to retrieve information about
  164. traces set on a given variable.
  165. The return value from \fBTcl_VarTraceInfo\fR is the \fIclientData\fR
  166. associated with a particular trace.
  167. The trace must be on the variable specified by the \fIinterp\fR,
  168. \fIvarName\fR, and \fIflags\fR arguments (only the TCL_GLOBAL_ONLY
  169. bit from \fIflags\fR is used;  other bits are ignored) and its trace procedure
  170. must the same as the \fIproc\fR argument.
  171. If the \fIprevClientData\fR argument is NULL then the return
  172. value corresponds to the first (most recently created) matching
  173. trace, or NULL if there are no matching traces.
  174. If the \fIprevClientData\fR argument isn't NULL, then it should
  175. be the return value from a previous call to \fBTcl_VarTraceInfo\fR.
  176. In this case, the new return value will correspond to the next
  177. matching trace after the one whose \fIclientData\fR matches
  178. \fIprevClientData\fR, or NULL if no trace matches \fIprevClientData\fR
  179. or if there are no more matching traces after it.
  180. This mechanism makes it possible to step through all of the
  181. traces for a given variable that have the same \fIproc\fR.
  182.  
  183. .SH "TWO-PART NAMES"
  184. .PP
  185. The procedures \fBTcl_TraceVar2\fR, \fBTcl_UntraceVar2\fR, and
  186. \fBTcl_VarTraceInfo2\fR are identical to \fBTcl_TraceVar\fR,
  187. \fBTcl_UntraceVar\fR, and \fBTcl_VarTraceInfo\fR, respectively,
  188. except that the name of the variable has already been
  189. separated by the caller into two parts.
  190. \fIName1\fR gives the name of a scalar variable or array,
  191. and \fIname2\fR gives the name of an element within an
  192. array.
  193. If \fIname2\fR is NULL it means that either the variable is
  194. a scalar or the trace is to be set on the entire array rather
  195. than an individual element (see WHOLE-ARRAY TRACES below for
  196. more information).
  197.  
  198. .SH "ACCESSING VARIABLES DURING TRACES"
  199. .PP
  200. During read and write traces, the
  201. trace procedure can read, write, or unset the traced
  202. variable using \fBTcl_GetVar2\fR, \fBTcl_SetVar2\fR, and
  203. other procedures.
  204. While \fIproc\fR is executing, traces are temporarily disabled
  205. for the variable, so that calls to \fBTcl_GetVar2\fR and
  206. \fBTcl_SetVar2\fR will not cause \fIproc\fR or other trace procedures
  207. to be invoked again.
  208. Disabling only occurs for the variable whose trace procedure
  209. is active;  accesses to other variables will still be traced.
  210. .VS
  211. However, if a variable is unset during a read or write trace then unset
  212. traces will be invoked.
  213. .VE
  214. .PP
  215. During unset traces the variable has already been completely
  216. expunged.
  217. It is possible for the trace procedure to read or write the
  218. variable, but this will be a new version of the variable.
  219. Traces are not disabled during unset traces as they are for
  220. read and write traces, but existing traces have been removed
  221. from the variable before any trace procedures are invoked.
  222. If new traces are set by unset trace procedures, these traces
  223. will be invoked on accesses to the variable by the trace
  224. procedures.
  225.  
  226. .SH "CALLBACK TIMING"
  227. .PP
  228. When read tracing has been specified for a variable, the trace
  229. procedure will be invoked whenever the variable's value is
  230. read.  This includes \fBset\fR Tcl commands, \fB$\fR-notation
  231. in Tcl commands, and invocations of the \fBTcl_GetVar\fR
  232. and \fBTcl_GetVar2\fR procedures.
  233. \fIProc\fR is invoked just before the variable's value is
  234. returned.
  235. It may modify the value of the variable to affect what
  236. is returned by the traced access.
  237. .VS
  238. If it unsets the variable then the access will return an error
  239. just as if the variable never existed.
  240. .VE
  241. .PP
  242. When write tracing has been specified for a variable, the
  243. trace procedure will be invoked whenever the variable's value
  244. is modified.  This includes \fBset\fR commands\fR,
  245. commands that modify variables as side effects (such as
  246. \fBcatch\fR and \fBscan\fR), and calls to the \fBTcl_SetVar\fR
  247. and \fBTcl_SetVar2\fR procedures).
  248. \fIProc\fR will be invoked after the variable's value has been
  249. modified, but before the new value of the variable has been
  250. returned.
  251. It may modify the value of the variable to override the change
  252. and to determine the value actually returned by the traced
  253. access.
  254. .VS
  255. If it deletes the variable then the traced access will return
  256. an empty string.
  257. .VE
  258. .PP
  259. When unset tracing has been specified, the trace procedure
  260. will be invoked whenever the variable is destroyed.
  261. The traces will be called after the variable has been
  262. completely unset.
  263.  
  264. .SH "WHOLE-ARRAY TRACES"
  265. .PP
  266. If a call to \fBTcl_TraceVar\fR or \fBTcl_TraceVar2\fR specifies
  267. the name of an array variable without an index into the array,
  268. then the trace will be set on the array as a whole.
  269. This means that \fIproc\fR will be invoked whenever any
  270. element of the array is accessed in the ways specified by
  271. \fIflags\fR.
  272. When an array is unset, a whole-array trace will be invoked
  273. just once, with \fIname1\fR equal to the name of the array
  274. and \fIname2\fR NULL;  it will not be invoked once for each
  275. element.
  276.  
  277. .SH "MULTIPLE TRACES"
  278. .PP
  279. It is possible for multiple traces to exist on the same variable.
  280. When this happens, all of the trace procedures will be invoked on each
  281. access, in order from most-recently-created to least-recently-created.
  282. When there exist whole-array traces for an array as well as
  283. traces on individual elements, the whole-array traces are invoked
  284. before the individual-element traces.
  285. .VS
  286. If a read or write trace unsets the variable then all of the unset
  287. traces will be invoked but the remainder of the read and write traces
  288. will be skipped.
  289. .VE
  290.  
  291. .SH "ERROR RETURNS"
  292. .PP
  293. Under normal conditions trace procedures should return NULL, indicating
  294. successful completion.
  295. If \fIproc\fR returns a non-NULL value it signifies that an
  296. error occurred.
  297. The return value must be a pointer to a static character string
  298. containing an error message.
  299. If a trace procedure returns an error, no further traces are
  300. invoked for the access and the traced access aborts with the
  301. given message.
  302. Trace procedures can use this facility to make variables
  303. read-only, for example (but note that the value of the variable
  304. will already have been modified before the trace procedure is
  305. called, so the trace procedure will have to restore the correct
  306. value).
  307. .PP
  308. The return value from \fIproc\fR is only used during read and
  309. write tracing.
  310. During unset traces, the return value is ignored and all relevant
  311. trace procedures will always be invoked.
  312.  
  313. .SH "RESTRICTIONS"
  314. .PP
  315. A trace procedure can be called at any time, even when there
  316. is a partically-formed result in the interpreter's result area.  If
  317. the trace procedure does anything that could damage this result (such
  318. as calling \fBTcl_Eval\fR) then it must save the original values of
  319. the interpreter's \fBresult\fR and \fBfreeProc\fR fields and restore
  320. them before it returns.
  321.  
  322. .SH "UNDEFINED VARIABLES"
  323. .PP
  324. It is legal to set a trace on an undefined variable.
  325. The variable will still appear to be undefined until the
  326. first time its value is set.
  327. If an undefined variable is traced and then unset, the unset will fail
  328. with an error (``no such variable''), but the trace
  329. procedure will still be invoked.
  330.  
  331. .SH "TCL_TRACE_DELETED FLAG"
  332. .PP
  333. In an unset callback to \fIproc\fR, the TCL_TRACE_DELETED bit
  334. is set in \fIflags\fR if the trace is being removed as part
  335. of the deletion.
  336. Traces on a variable are always removed whenever the variable
  337. is deleted;  the only time TCL_TRACE_DELETED isn't set is for
  338. a whole-array trace invoked when only a single element of an
  339. array is unset.
  340.  
  341. .SH "TCL_INTERP_DESTROYED"
  342. .PP
  343. When an interpreter is destroyed, unset traces are called for
  344. all of its variables.
  345. The TCL_INTERP_DESTROYED bit will be set in the \fIflags\fR
  346. argument passed to the trace procedures.
  347. Trace procedures must be extremely careful in what they do if
  348. the TCL_INTERP_DESTROYED bit is set.
  349. It is not safe for the procedures to invoke any Tcl procedures
  350. on the interpreter, since its state is partially deleted.
  351. All that trace procedures should do under these circumstances is
  352. to clean up and free their own internal data structures.
  353.  
  354. .SH BUGS
  355. .PP
  356. Tcl doesn't do any error checking to prevent trace procedures
  357. from misusing the interpreter during traces with TCL_INTERP_DESTROYED
  358. set.
  359.  
  360. .SH KEYWORDS
  361. clientData, trace, variable
  362.